package com.quanquan.user.repository;

import com.quanquan.user.entity.IntegralTradingDetails;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * 功能描述:积分交易管理 （新建，删除，更新） @ClassName: UserGroupRepository @Author: xiaoqianh丶 @Date: 2019-03-19
 * 14:59 @Version: V1.0
 */
public interface IntegralDetailsRepository
        extends JpaRepository<IntegralTradingDetails, String>,
        JpaSpecificationExecutor<IntegralTradingDetails> {

    /**
     * @return 根据用户id查询积分消费明细
     */
    public List<IntegralTradingDetails> findIntegralTradingDetailsByUserIdAndTradeAction(
            String userId, Integer tradeAction);

    public List<IntegralTradingDetails> findIntegralTradingDetailsByUserId(
            String userId);

    @Query(value = "SELECT * FROM oo_integral_trading_details WHERE trade_action = 0", nativeQuery = true)
    List<IntegralTradingDetails> findAllIn();

    @Query(value = "SELECT * FROM oo_integral_trading_details WHERE trade_action = 1", nativeQuery = true)
    List<IntegralTradingDetails> findAllOut();



    /**
     *指定时间段内的积分获得数
     * @param starTime
     * @param endTime
     * @return
     */
    @Query(value = "select sum(integral_point)  from oo_integral_trading_details where trade_action= 0 and create_time BETWEEN ?1 and ?2", nativeQuery = true)
    Integer findIntegralByNum(Date starTime, Date endTime);

    /**
     *指定时间段内的积分获得数
     * @param starTime
     * @param endTime
     * @return
     */
    @Query(value = "select sum(integral_point)  from oo_integral_trading_details where trade_action= 1 and create_time BETWEEN ?1 and ?2", nativeQuery = true)
    Integer findIntegralConsumeByNum(Date starTime, Date endTime);


}
